﻿using Studio77.Timer.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace Studio77.Timer
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public static MainPage instance;
        object itemSyncObj = new object();
        public TimerItemCollection timerItems = new TimerItemCollection();
        int tipNameCount = 1;
        Random r = new Random(DateTime.Now.Millisecond);
        DispatcherTimer timer = new DispatcherTimer();

        public MainPage()
        {
            this.InitializeComponent();
            instance = this;
            this.Loaded += MainPage_Loaded;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= MainPage_Loaded;
            this.PointerPressed += bdrTimerSeter_PointerPressed;
            this.PointerReleased += bdrTimerSeter_PointerReleased;
            this.selectMinute.SetValue(5);
            this.lstTimers.DataContext = timerItems;
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, object e)
        {
            lock (itemSyncObj)
            {
                List<TimerItemViewModel> timerList = new List<TimerItemViewModel>();

                foreach (var item in timerItems)
                {
                    if (item.IsStoped)
                    {
                        continue;
                    }
                    item.LeftSeconds--;
                    item.LeftTime = GetLeftTimeStr(item.LeftSeconds);

                    if (item.LeftSeconds <= 0)
                    {
                        item.TxtBrush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
                        item.IsStoped = true;
                        //提示时间到
                        AudioPlayHelper.PlayEffectSound("ring.mp3");
                        timerList.Add(item);
                    }
                }

                foreach (var item in timerList)
                {
                    timerItems.Remove(item);
                    timerItems.Insert(0, item);
                }
            }
        }

        private string GetLeftTimeStr(int seconds)
        {
            string hour = (seconds / 3600).ToString("D2");
            string minute = ((seconds % 3600) / 60).ToString("D2");
            string second = (seconds % 60).ToString("D2");
            return hour + ":" + minute + ":" + second;
        }

        Point start = new Point();
        Point end = new Point();
        Stopwatch s = new Stopwatch();

        void bdrTimerSeter_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            s.Stop();
            long time = s.ElapsedMilliseconds;
            end = e.GetCurrentPoint(bdrTimerSeter).Position;
            double angle = 0;
            if (Math.Abs(end.X - start.X) < 1 && Math.Abs(end.Y - start.Y) < 1)
            {
                angle = 0;
            }
            else if (end.X > start.X)
            {

                if (end.Y > start.Y)
                {
                    angle = 360 - Math.Atan((end.Y - start.Y) * 1.0 / (end.X - start.X)) * 180 / Math.PI;
                }
                else
                {
                    angle = Math.Atan((start.Y - end.Y) * 1.0 / (end.X - start.X)) * 180 / Math.PI;
                }
            }
            else if (end.X < start.X)
            {
                if (end.Y > start.Y)
                {
                    angle = Math.Atan((end.Y - start.Y) * 1.0 / (start.X - end.X)) * 180 / Math.PI + 180;
                }
                else
                {
                    angle = 180 - Math.Atan((start.Y - end.Y) * 1.0 / (start.X - end.X)) * 180 / Math.PI;
                }
            }

            double length = Math.Sqrt((end.X - start.X) * (end.X - start.X) + (end.Y - start.Y) * (end.Y - start.Y));
            Debug.WriteLine("速度" + length / time);
            if (angle == 0.0)
            {
                //点击

                return;
            }
            else if (angle < 135 || angle > 45)
            {

                //down
                if (start.Y < 123)
                {
                    //新增一个计时器
                    //MessageDialog md = new MessageDialog("down");
                    //md.ShowAsync();

                    TimerItemViewModel model = new TimerItemViewModel();
                    model.TipName = tipNameCount.ToString();
                    tipNameCount++;
                    int color = r.Next(3);
                    model.TipBrush = new SolidColorBrush(Color.FromArgb(255, color == 0 ? (byte)r.Next(170, 255) : (byte)50, color == 1 ? (byte)r.Next(170, 255) : (byte)50, color == 2 ? (byte)r.Next(170, 255) : (byte)50));
                    model.LeftSeconds = GetLeftTime();
                    model.LeftTime = GetLeftTimeStr();
                    model.TxtBrush = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));

                    lock (itemSyncObj)
                    {
                        timerItems.Add(model);
                    }
                }

            }
        }

        private string GetLeftTimeStr()
        {
            string hour = selectHour.Value.ToString("D2");
            string minute = selectMinute.Value.ToString("D2");
            string second = selectSecond.Value.ToString("D2");
            return hour + ":" + minute + ":" + second;

        }

        private int GetLeftTime()
        {
            int hour = selectHour.Value;
            int minute = selectMinute.Value;
            int second = selectSecond.Value;
            return hour * 3600 + minute * 60 + second;

        }

        void bdrTimerSeter_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            start = e.GetCurrentPoint(this).Position;
            s.Restart();

        }



        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}
